package com.stationbuilding.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.stationbuilding.MyApplication;
import com.stationbuilding.R;
import com.stationbuilding.adapter.SelectListAdapter;
import com.stationbuilding.adapter.member.MemberFragmentPagerAdapter;
import com.stationbuilding.api.RetrofitManager;
import com.stationbuilding.bean.DictBean;
import com.stationbuilding.bean.DictListBean;
import com.stationbuilding.bean.ProcedureBean;
import com.stationbuilding.bean.ProcedureListBean;
import com.stationbuilding.bean.QuestionTypeBean;
import com.stationbuilding.bean.QuestionTypeListBean;
import com.stationbuilding.bean.SubdivisionBean;
import com.stationbuilding.bean.SubdivisionListBean;
import com.stationbuilding.bean.UserBean;
import com.stationbuilding.bean.UserListBean;
import com.stationbuilding.constants.Constants;
import com.stationbuilding.fragment.member.MemberFragmentCompany;
import com.stationbuilding.fragment.member.MemberFragmentGroup;
import com.stationbuilding.fragment.member.MemberFragmentMember;
import com.stationbuilding.utils.DialogUtils;
import com.stationbuilding.utils.PreferenceHelper;
import com.stationbuilding.utils.TabLayoutUtil;
import com.zhy.tree.bean.Node;
import com.zhy.tree.bean.TreeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class MemberSelectFragment extends Fragment {

    private View view;
    private TabLayout tabLayout;
    private int currentPosition;
    private List<Fragment> fragmentList = new ArrayList<>();
    private LinearLayout search;
    private EditText searchEdit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_member_select, container, false);
        searchEdit = (EditText) view.findViewById(R.id.search_edit);
        search = (LinearLayout) view.findViewById(R.id.search);
        initTabLayout();
        initImageView();
        return view;
    }

    /**
     * 初始化搜索图片按钮
     */
    private void initImageView() {
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search.setVisibility(View.GONE);
                searchEdit.setVisibility(View.VISIBLE);
                searchEdit.requestFocus();
                InputMethodManager inputMethodManager = (InputMethodManager)searchEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            }
        };

        // 根据输入框输入值的改变来过滤搜索
        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
                Fragment fragment = fragmentList.get(currentPosition);
                if (currentPosition == 0) {
                    ((MemberFragmentMember) fragment).filterData(s.toString());
                } else if (currentPosition == 1) {
                    ((MemberFragmentGroup) fragment).filterData(s.toString());
                } else {
                    ((MemberFragmentCompany) fragment).filterData(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        search.setOnClickListener(onClickListener);
    }

    /**
     * 初始化页签
     */
    private void initTabLayout() {
        ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                search.setVisibility(View.VISIBLE);
                searchEdit.setVisibility(View.GONE);
                searchEdit.setText("");
                InputMethodManager inputMethodManager = (InputMethodManager)searchEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
                // 把当前显示的position传递出去
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        };
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        fragmentList.add(new MemberFragmentMember(true));
        fragmentList.add(new MemberFragmentGroup(true));
        fragmentList.add(new MemberFragmentCompany(true));
        viewPager.setAdapter(new MemberFragmentPagerAdapter(getChildFragmentManager(), fragmentList));
        viewPager.addOnPageChangeListener(onPageChangeListener);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                TabLayoutUtil.setIndicator(tabLayout, 20, 20);
            }
        });
    }
}
